package com.goodving.guli.bt;

import android.content.Context;
import android.content.Intent;
import android.util.SparseArray;

import com.goodving.guli.R;
import com.goodving.guli.bt.eventbus.EventWeightSyncedDevice;
import com.goodving.guli.bt.model.Birthday;
import com.goodving.guli.bt.model.C1981P;
import com.goodving.guli.bt.model.PersonInfo;
import com.goodving.guli.bt.model.WeightInfo;
import com.goodving.guli.bt.model.WeightInfoManager;
import com.goodving.guli.bt.profile.scale.C0638c;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.profile.scale.C3933m;
import com.goodving.guli.bt.profile.scale.ScaleWeightInfo;
import com.goodving.guli.bt.utils.WeightUtil;
import com.goodving.guli.bt.weight.LuaUtil;
import com.goodving.guli.bt.weight.UserInfo;
import com.goodving.guli.bt.weight.UserInfoManager;
import com.goodving.guli.model.Keeper;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by john on 2015/12/15.
 */
public class SyncWeightDataCB extends C0638c { // C0647L
    public static final int f3467a = 30000;
    private static final String f3468b = "SyncWeightDataCB";
    private SparseArray<ArrayList<WeightInfo>> f3469d;
    private int f3470e;
    private Context f3471f;

    public SyncWeightDataCB(Context context) {
        this.f3469d = new SparseArray();
        this.f3470e = 0;
        this.f3471f = null;
        this.f3471f = context;
    }

    private int m7938a(ArrayList<ScaleWeightInfo> arrayList) {
        if (arrayList == null || arrayList.size() <= 0) {
            return 0;
        }
        List a = null;
        WeightInfoManager a2 = WeightInfoManager.getInstance();
        UserInfoManager a3 = UserInfoManager.getInstance();
        ArrayList arrayList2 = new ArrayList();
        boolean weightMergeResult = Keeper.readPersonInfo().getMiliConfig().getWeightMergeResult();
        Iterator it = arrayList.iterator();
        int i = 0;
        while (it.hasNext()) {
            int i2;
            ScaleWeightInfo ScaleWeightInfo = (ScaleWeightInfo) it.next();
            WeightInfo a4 = a2.m15745a(ScaleWeightInfo.m25494e(), true);
            UserInfo userInfo = null;
            if (a4 != null) {
                if (WeightInfoManager.m15737g(a4)) {
                    a2.deleteInfoLocal(a4);
                } else {
                    userInfo = a3.getInfoByUid(a4.uid);
                    if (userInfo == null) {
                        GuliLog.m12370d(f3468b, "Should not go here!!!uid:" + a4.uid);
                    }
                }
            }
            if (userInfo == null) {
                List a5 = WeightUtil.m15701a(ScaleWeightInfo);
                if (a5 == null) {
                    GuliLog.m12370d(f3468b, "Drop no matching weight data!!!");
                } else if (a5.size() != 1) {
                    GuliLog.m12370d(f3468b, "Drop the weight data that matched more than one user!!!");
                } else {
                    userInfo = (UserInfo) a5.get(0);
                }
            }
            WeightInfo weightInfo = null;
            if (userInfo == null) {
                weightInfo = new WeightInfo(ScaleWeightInfo.m25500k(), -1, ScaleWeightInfo.m25494e());
            } else {
                weightInfo = new WeightInfo(ScaleWeightInfo.m25500k(), userInfo.uid, ScaleWeightInfo.m25494e());
            }
            weightInfo.deviceid = ScaleWeightInfo.m25481a();
            if (weightMergeResult) {
                m7940a(weightInfo);
                i2 = i;
            } else {
                i2 = i + 1;
                if (a4 != null) {
                    i2--;
                    if (a4.synced == 2) {
                        a2.deleteInfoLocal(a4);
                        arrayList2.add(weightInfo);
                    }
                } else {
                    arrayList2.add(weightInfo);
                }
            }
            i = i2;
        }
        if (weightMergeResult) {
            this.f3470e = 0;
            a = m7939a(this.f3469d);
            i = this.f3470e;
        } else {
            a = arrayList2;
        }
        a2.m15756a(a);
        if (a.size() <= 0) {
            return i;
        }
        EventBus.getDefault().post(new EventWeightSyncedDevice(true));
        return i;
    }

    private ArrayList<WeightInfo> m7939a(SparseArray<ArrayList<WeightInfo>> sparseArray) {
        ArrayList<WeightInfo> arrayList = new ArrayList();
        int size = sparseArray.size();
        WeightInfoManager a = WeightInfoManager.getInstance();
        WeightInfo weightInfo2 = null;
        for (int i = 0; i < size; i++) {
            WeightInfo weightInfo = null;
            Iterator it = ((ArrayList) sparseArray.valueAt(i)).iterator();
            while (it.hasNext()) {
                weightInfo2 = (WeightInfo) it.next();
                if (weightInfo2.uid == 0) {
                    GuliLog.m12370d(f3468b, "<<merge>> USER_ID_UNKNOWN:" + weightInfo2);
                    arrayList.add(weightInfo2);
                    weightInfo2 = weightInfo;
                } else if (weightInfo == null) {
                    GuliLog.m12370d(f3468b, "<<merge>> lastInfo is null");
                } else if (Math.abs(weightInfo2.timestamp - weightInfo.timestamp) <= 30000) {
                    GuliLog.m12370d(f3468b, "<<merge>> ignore last info");
                    weightInfo = a.m15760b(weightInfo.timestamp);
                    if (weightInfo != null) {
                        GuliLog.m12370d(f3468b, "<<merge>> delete last info");
                        a.deleteInfoLocal(weightInfo);
                    }
                } else {
                    WeightInfo b = a.m15760b(weightInfo.timestamp);
                    if (b == null) {
                        this.f3470e++;
                        GuliLog.m12370d(f3468b, "<<merge>> add last info:" + weightInfo);
                    } else {
                        a.deleteInfoLocal(b);
                        GuliLog.m12370d(f3468b, "<<merge>> exsit last info:" + weightInfo);
                    }
                    arrayList.add(weightInfo);
                }
                weightInfo = weightInfo2;
            }
            if (weightInfo != null) {
                weightInfo2 = a.m15760b(weightInfo.timestamp);
                if (weightInfo2 == null) {
                    this.f3470e++;
                    GuliLog.m12370d(f3468b, "<<merge>> add last info:" + weightInfo);
                } else {
                    a.deleteInfoLocal(weightInfo2);
                    GuliLog.m12370d(f3468b, "<<merge>> exsit last info:" + weightInfo);
                }
                arrayList.add(weightInfo);
            }
        }
        return arrayList;
    }

    private void m7940a(WeightInfo weightInfo) {
        ArrayList arrayList = (ArrayList) this.f3469d.get(weightInfo.uid);
        if (arrayList == null) {
            arrayList = new ArrayList();
            this.f3469d.put(weightInfo.uid, arrayList);
            GuliLog.m12370d(f3468b, "<<merge>> add to SparseArray:" + weightInfo.uid);
        }
        GuliLog.m12370d(f3468b, "<<merge>> add to WeightInfo list:" + weightInfo);
        arrayList.add(weightInfo);
    }

    private void m7941b() {
        float f = -1;
        GuliLog.tracePrintInfo(f3468b, "in updateWeightUI");
        UserInfoManager.getInstance().m15598d();
        WeightInfoManager.getInstance();
        WeightInfo h = WeightInfoManager.getInstance().m15782h();
        UserInfo a = UserInfoManager.getInstance().getInfoByUid(h.uid);
        C1981P c1981p = new C1981P();
        c1981p.weightInfo.setDate(h.timestamp);
        c1981p.weightInfo.setStable(true);
        c1981p.weightInfo.setFinish(false);
        PersonInfo readPersonInfo = Keeper.readPersonInfo();
        c1981p.weightInfo.setUnitType(readPersonInfo.miliConfig.weightUnit);
        c1981p.weightInfo.m25488b(WeightUtil.m15706b(h.weight, readPersonInfo.miliConfig.weightUnit));
        c1981p.weightInfo.m25482a(a != null ? WeightUtil.m15693a(a.height, Birthday.fromStr(a.birthday).getAge(), a.weight) : -1);
        c1981p.f11134b = a != null ? a.name : this.f3471f.getString(R.string.visitor);
        c1981p.f11135c = a != null ? a.uid : 0;
        if (a != null) {
            f = a.targetWeight;
        }
        c1981p.f11136d = f;
        EventBus.getDefault().post(c1981p);
    }

    public void m7942a() {
    }

    public void m7943a(Context context) {
        if (context == null) {
            throw new IllegalArgumentException();
        }
        GuliLog.tracePrintInfo(f3468b, "notifyDynamicStatusChanged");
        context.sendBroadcast(new Intent(BLEService.mStrDynamicStatusChanged));
    }

    public void m7944a(C3933m c3933m) {
        int i = 0;
        if (c3933m.f19826d != null) {
            i = m7938a(c3933m.f19826d);
        }
        if (i > 0) {
            m7941b();
            LuaUtil.getInstance(this.f3471f).m15962a(c3933m, i);
            m7943a(this.f3471f);
            C0668a.m8126a(this.f3471f, C0668a.fK);
            if (c3933m.f19825c > 0) {
                GuliLog.tracePrintInfo(f3468b, " reboot Count " + c3933m.f19825c);
                C0668a.m8127a(this.f3471f, C0668a.fP, c3933m.f19825c);
            }
        }
    }

    public void m7912a(Object obj) {  // m7945a
        super.m7912a(obj);
        if (obj != null) {
            m7944a((C3933m) obj);
        }
    }
}
